/**
 * 服务
 * */
package org.zmhhxl.user.service.impl;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.zmhhxl.api.auth.domain.entity.ThirdPartyUser;
import org.zmhhxl.user.domain.repository.ThirdPartyUserRepository;
import org.zmhhxl.user.service.ThirdPartyUserService;

import java.util.Optional;


@Service
@AllArgsConstructor
//@Transactional(rollbackFor = Exception.class)
public class JpaThirdPartyUserServiceImpl implements ThirdPartyUserService {

   private final ThirdPartyUserRepository thirdPartyUserRepository;

   @Override
   public Optional<ThirdPartyUser> findById(Long id) {
      //ThirdPartyUser thirdPartyUser = thirdPartyUserRepository.findById(id).orElse(null);
      return Optional.of(thirdPartyUserRepository.findById(id).orElse(null));
//      if (thirdPartyUser != null){
//         // 显式初始化集合
//         //Hibernate.initialize(thirdPartyUser.getUserInfos());
//         return thirdPartyUser;
//      }else
//      return null;
   }

//   @Override public Result<ThirdPartyUser> findByIdR(Long id) {
//      ThirdPartyUser thirdPartyUser = thirdPartyUserRepository.findById(id).orElse(null);
//      if (thirdPartyUser != null){
//         // 显式初始化集合
//         //Hibernate.initialize(thirdPartyUser.getUserInfos());
//         return Result.success(thirdPartyUser);
//      }else
//         return Result.fail();
//   }
//
//   @Override public Result<ThirdPartyUser> findByThirdPartyIdAndThirdPartyDomainR(String thirdPartyId, String domain) {
//      ThirdPartyUser thirdPartyUser = thirdPartyUserRepository.findByThirdPartyIdAndThirdPartyDomain( thirdPartyId,  domain);
//      if (thirdPartyUser != null){
//         // 显式初始化集合
//         //Hibernate.initialize(thirdPartyUser.getUserInfos());
//         return Result.success(thirdPartyUser);
//      }else
//         return Result.fail();
//   }

   @Override
   public ThirdPartyUser findByThirdPartyIdAndThirdPartyType(String thirdPartyId, String type) {

      return thirdPartyUserRepository.findByThirdPartyIdAndThirdPartyType(thirdPartyId, type);
   }

   @Override
   //@Transactional(readOnly = true)
   public Optional<ThirdPartyUser> findByThirdPartyIdAndThirdPartyDomain(String thirdPartyId, String domain) {
      Optional<ThirdPartyUser> thirdPartyUser = thirdPartyUserRepository.findByThirdPartyIdAndThirdPartyDomain( thirdPartyId,  domain);
//      if (thirdPartyUser != null){
//         // 显式初始化集合
//         //Hibernate.initialize(thirdPartyUser.getUserInfos());
//         return thirdPartyUser;
//      }else
//         return null;
      return thirdPartyUser;
   }

   @Override
   public ThirdPartyUser save(ThirdPartyUser thirdPartyUser) {
      return thirdPartyUserRepository.save(thirdPartyUser);
   }

}
